package cn.chair.dao;

import cn.chair.entity.Order;
import cn.chair.model.admin.*;
import life.innovation.dao.EntityDaoImpl;
import life.innovation.exception.DataReadException;
import life.innovation.model.Page;
import life.innovation.model.PageList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class OrderDao extends EntityDaoImpl<Order>
{
    private static final Logger elog = LoggerFactory.getLogger("errorLogger");

    public OrderDao(DataSource dataSource) {
        super(dataSource, Order.class);
    }

    /**
     * 订单信息
     * @param orderNo
     * @param startDate
     * @param endDate
     * @param orderStatus
     * @param chairCode
     * @param merchantName
     * @param page
     * @param countPerPage
     * @return
     * @throws DataReadException
     */
    public PageList<PurchaseOrderResp.OrderList> getOrderList(String transNo, String orderNo, String startDate, String endDate, Integer orderStatus,
                                                              String chairCode, String merchantName,Integer paySource, int page, int countPerPage,List<Integer> storeId) throws DataReadException {
            Map<String, Object> params = new HashMap<>();
        orderNo = (orderNo==null||orderNo.trim().isEmpty())?null:orderNo.trim();
        transNo = (transNo==null||transNo.trim().isEmpty())?null:transNo.trim();
        startDate = (startDate==null||startDate.trim().isEmpty())?null:startDate.trim();
             endDate = (endDate==null||endDate.trim().isEmpty())?null:endDate.trim();
              chairCode = (chairCode==null||chairCode.trim().isEmpty())?null:chairCode.trim();
              merchantName = (merchantName ==null||merchantName.trim().isEmpty())?null:"%"+merchantName+"%";

              List<Integer> sourceList = new ArrayList<>();
             if(paySource == null){
                 sourceList.add(0);
                 sourceList.add(2);
             }else{
                 sourceList.add(paySource);
             }

            params.put("startDate", startDate);
            params.put("endDate", endDate);
            params.put("orderStatus", orderStatus);
            params.put("orderNo", orderNo);
            params.put("transNo", transNo);
            params.put("chairCode", chairCode);
            params.put("merchantName", merchantName);
            params.put("paySource",sourceList);
            params.put("storeId",storeId);
        if(storeId!=null &&storeId.size()>0){
            params.put("store",storeId.get(0));
        }else{
            params.put("store",null);
        }

            return findPage("db/sql/order/getMerchantOrderList.sql", PurchaseOrderResp.OrderList.class, page, countPerPage, params);
    }

    public List<PurchaseOrderSummaryResp> getOrderSummary(String orderNo, String startDate, String endDate, Integer orderStatus,
                                                    String chairCode, String merchantName, Integer paySource,List<Integer> storeId) throws DataReadException {
        Map<String, Object> params = new HashMap<>();
        orderNo = (orderNo==null||orderNo.trim().isEmpty())?null:orderNo.trim();
        startDate = (startDate==null||startDate.trim().isEmpty())?null:startDate.trim();
        endDate = (endDate==null||endDate.trim().isEmpty())?null:endDate.trim();
        chairCode = (chairCode==null||chairCode.trim().isEmpty())?null:chairCode.trim();
        merchantName = (merchantName ==null||merchantName.trim().isEmpty())?null:"%"+merchantName+"%";

        List<Integer> sourceList = new ArrayList<>();
        if(paySource == null){
            sourceList.add(0);
            sourceList.add(2);
        }else{
            sourceList.add(paySource);
        }

        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("orderStatus", orderStatus);
        params.put("orderNo", orderNo);
        params.put("chairCode", chairCode);
        params.put("merchantName", merchantName);
        params.put("paySource",sourceList);
        params.put("storeId",storeId);
        if(storeId!=null &&storeId.size()>0){
            params.put("store",storeId.get(0));
        }else{
            params.put("store",null);
        }

        return find("db/sql/order/getMerchantOrderSummary.sql", PurchaseOrderSummaryResp.class, params);
    }

    /**
     * 订单表格导出信息
     * @param orderNo
     * @param startDate
     * @param endDate
     * @param orderStatus
     * @param chairCode
     * @param merchantName
     * @return
     * @throws DataReadException
     */
    public List<PurchaseOrderResp.OrderList> getOrderExcelList(String orderNo, String startDate, String endDate, Integer orderStatus,
                                                              String chairCode, String merchantName,Integer paySource,List<Integer> storeId) throws DataReadException {



            Map<String, Object> params = new HashMap<>();

            orderNo = (orderNo==null||orderNo.trim().isEmpty())?null:orderNo.trim();
            startDate = (startDate==null||startDate.trim().isEmpty())?null:startDate.trim();
            endDate = (endDate==null||endDate.trim().isEmpty())?null:endDate.trim();
            chairCode = (chairCode==null||chairCode.trim().isEmpty())?null:chairCode.trim();
            merchantName = (merchantName ==null||merchantName.trim().isEmpty())?null:"%"+merchantName+"%";

            List<Integer> sourceList = new ArrayList<>();
            if(paySource == null){
               sourceList.add(0);
               sourceList.add(2);
            }else{
               sourceList.add(paySource);
            }

            params.put("startDate", startDate);
            params.put("endDate", endDate);
            params.put("orderStatus", orderStatus);
            params.put("orderNo", orderNo);
            params.put("chairCode", chairCode);
            params.put("merchantName", merchantName);
            params.put("paySource",sourceList);
            params.put("storeId",storeId);

        if(storeId!=null &&storeId.size()>0){
            params.put("store",storeId.get(0));
        }else{
            params.put("store",null);
        }
            return find("db/sql/order/getMerchantOrderList.sql", PurchaseOrderResp.OrderList.class, params);

    }

    /**
     * 门店结算报表
     * @param merchantId
     * @param merchantName
     * @param startDate
     * @param endDate
     * @param regionId
     * @return
     * @throws DataReadException
     */
    public List<MerchantOrderResp.MerchantOrderList> getOrders(Integer merchantId,String merchantName,
                                                               String startDate,String endDate,Integer regionId,List<Integer> storeId) throws DataReadException {

        Map<String,Object> params = new HashMap<>();
        merchantName= (merchantName==null||merchantName.trim().isEmpty())?null:"%"+merchantName.trim()+"%";
        startDate = (startDate==null||startDate.trim().isEmpty())?null:startDate.trim();
        endDate = (endDate==null||endDate.trim().isEmpty())?null:endDate.trim();

        params.put("merchantId",merchantId);
        params.put("merchantName",merchantName);
        params.put("startDate",startDate);
        params.put("endDate",endDate);
        params.put("regionId",regionId);
        params.put("storeId",storeId);
        if(storeId!=null &&storeId.size()>0){
            params.put("store",storeId.get(0));
        }else{
            params.put("store",null);
        }
        return find("db/sql/order/getMerchantOrder.sql",MerchantOrderResp.MerchantOrderList.class,params);

    }

    /**
     * 储值汇总
     * @param startDate
     * @param endDate
     * @return
     * @throws DataReadException
     */
    public List<RechargeOrderResp> getRechargeOrder(String startDate,String endDate) throws DataReadException {
        Map<String,Object> params = new HashMap<>();
        startDate = (startDate==null||startDate.trim().isEmpty())?null:startDate.trim();
        endDate = (endDate==null||endDate.trim().isEmpty())?null:endDate.trim();

        params.put("startDate",startDate);
        params.put("endDate",endDate);
        return find("db/sql/order/getRechargeOrder.sql",RechargeOrderResp.class,params);
    }

    /**
     * 储值详情订单
     * @param startDate
     * @param endDate
     * @param page
     * @param countPerPage
     * @return
     * @throws DataReadException
     */
    public PageList<RechargeOrderListResp.RechargeOrderList> getRechargeOrderList(String startDate, String endDate, int page, int countPerPage) throws DataReadException {
        Map<String,Object> params = new HashMap<>();
        startDate = (startDate==null||startDate.trim().isEmpty())?null:startDate.trim();
        endDate = (endDate==null||endDate.trim().isEmpty())?null:endDate.trim();
        params.put("startDate",startDate);
        params.put("endDate",endDate);

        return findPage("db/sql/order/getRechargeOrderList.sql",RechargeOrderListResp.RechargeOrderList.class,page,countPerPage,params);
    }

    /**
     * 储值详情报表导出数据
     * @param startDate
     * @param endDate
     * @return
     * @throws DataReadException
     */
    public List<RechargeOrderListResp.RechargeOrderList> getExcelRechargeOrderList(String startDate, String endDate) throws DataReadException {
        Map<String,Object> params = new HashMap<>();
        startDate = (startDate==null||startDate.trim().isEmpty())?null:startDate.trim();
        endDate = (endDate==null||endDate.trim().isEmpty())?null:endDate.trim();
        params.put("startDate",startDate);
        params.put("endDate",endDate);

        return find("db/sql/order/getRechargeOrderList.sql",RechargeOrderListResp.RechargeOrderList.class,params);
    }
}
